package com.leo.blog.web.black;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.leo.blog.login.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dto.BlackIpExecution;
import com.leo.blog.entity.BlackIp;
import com.leo.blog.entity.Bloger;
import com.leo.blog.enums.BlackIpStateEnum;
import com.leo.blog.service.BlackIpService;
import com.leo.blog.service.VisitService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/black")
public class BlackIpManagementController {
	@Autowired
	private BlackIpService blackIpService;
	@Autowired
	private VisitService visitService;
	
	/**
	 * 删除黑名单功能
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteblackipbyvid")
	@ResponseBody
	public Map<String, Object> deleteBlackIpByVid(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取传过来的值
		int blackVid = HttpServletRequestUtil.getInt(request, "blackVid");
		
		//获取session
		Bloger bloger = SecurityUtils.getBloger();
		if(bloger.getHasPermission() == 1) {
			if(blackVid > 0) {
				BlackIpExecution be = blackIpService.deleteBlackIpByVid("撤销移除Ip", blackVid);
				if(be.getState() == BlackIpStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "删除失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败, id错误");
			}	
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		
		return modelMap;
	}
	
	/**
	 * 查询黑名单的数目
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getallblackipcount")
	@ResponseBody
	public Map<String, Object> getAllBlackIpCount(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		BlackIpExecution be = blackIpService.getAllBlackIpCount();
		if(be.getState() == BlackIpStateEnum.SUCCESS.getState()) {
			modelMap.put("allCount", be.getAllCount());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "获取黑名单数目失败");
		}
		
		return modelMap;
	}
	
	/**
	 * 添加黑名单功能
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addblackip")
	@ResponseBody
	@Transactional
	public Map<String, Object> addBlackIp(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blackVid = HttpServletRequestUtil.getInt(request, "blackVid");
		String blackIp = HttpServletRequestUtil.getString(request, "blackIp");		
		String city = HttpServletRequestUtil.getString(request, "city");
		String browserType = HttpServletRequestUtil.getString(request, "browserType");
		String platformType = HttpServletRequestUtil.getString(request, "platformType");
		
		BlackIp black = new BlackIp();
		//判断是否已经存在该ip
		BlackIpExecution beIp = blackIpService.getBlackIpByIp(blackIp);
		if(beIp.getBlackIp() != null) {
			//已经存在
			modelMap.put("cunzai", 2);
		}else {
			if(blackVid > 0) {
				black.setBlackVid(blackVid);
			}
			if(blackIp != null && blackIp != "") {
				black.setBlackIp(blackIp);
			}
			if(city != null && city != "") {
				black.setCity(city);
			}
			if(browserType != null && browserType != "") {
				black.setBrowserType(browserType);
			}
			if(platformType != null && platformType != "") {
				black.setPlatformType(platformType);
			}
			//获取session
			Bloger bloger = SecurityUtils.getBloger();
			
			if(bloger.getHasPermission() == 1) {
				BlackIpExecution be = blackIpService.addBlackIp("拉黑ip", black);
				if(be.getState() == BlackIpStateEnum.SUCCESS.getState()) {
					visitService.deleteVisitById(blackVid);
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "添加信息失败");
				}	
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "无权限操作");
			}
			
			
		}
		return modelMap;
	}
	
	/**
	 * 更新黑名单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateblackipbyid")
	@ResponseBody
	public Map<String, Object> updateBlackIpById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blackId = HttpServletRequestUtil.getInt(request, "blackId");
		int blackVid = HttpServletRequestUtil.getInt(request, "blackVid");
		String blackIp = HttpServletRequestUtil.getString(request, "blackIp");		
		String city = HttpServletRequestUtil.getString(request, "city");
		String browserType = HttpServletRequestUtil.getString(request, "browserType");
		String platformType = HttpServletRequestUtil.getString(request, "platformType");
		
		BlackIp black = new BlackIp();
		//判断
		if(blackId > 0) {
			black.setBlackId(blackId);
			if(blackVid > 0) {
				black.setBlackVid(blackVid);
			}
			if(blackIp != null && blackIp != "") {
				black.setBlackIp(blackIp);
			}
			if(city != null && city != "") {
				black.setCity(city);
			}
			if(browserType != null && browserType != "") {
				black.setBrowserType(browserType);
			}
			if(platformType != null && platformType != "") {
				black.setPlatformType(platformType);
			}

			Bloger bloger = SecurityUtils.getBloger();
			
			if(bloger.getHasPermission() == 1) {
				BlackIpExecution be = blackIpService.updateBlackIpByIpSelective(black);
				if(be.getState() == BlackIpStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "无权限操作");
			}
			
			
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "更新失败,id不存在");
		}
		
		return modelMap;
	}
	
	/**
	 * 模糊组合分页查询信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getblackiplistbypage")
	@ResponseBody
	public Map<String, Object> getBlackIpListByPage(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		String blackIp = HttpServletRequestUtil.getString(request, "blackIp");
		String city = HttpServletRequestUtil.getString(request, "city");
		String browserType = HttpServletRequestUtil.getString(request, "browserType");
		String platformType = HttpServletRequestUtil.getString(request, "platformType");
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		
		BlackIp blackIpCondition = new BlackIp();
		//判断
		if(blackIp != null && blackIp != "") {
			blackIpCondition.setBlackIp(blackIp);
		}
		if(city != null && city != "") {
			blackIpCondition.setCity(city);
		}
		if(browserType != null && browserType != "") {
			blackIpCondition.setBrowserType(browserType);
		}
		if(platformType != null && platformType != "") {
			blackIpCondition.setPlatformType(platformType);
		}
		if(startTime != null && startTime != "") {
			blackIpCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			blackIpCondition.setEndTime(endTime);
		}
		
		BlackIpExecution be = blackIpService.getBlackIpListByPage(blackIpCondition, pageIndex, pageSize);
		
		if(be.getState() == BlackIpStateEnum.SUCCESS.getState()) {
			//返回页数
			int page = 0;
			int pageCount = be.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			
			modelMap.put("blackIpList", be.getBlackIpList());
			modelMap.put("count", be.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
			
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
}
